<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/8
 * Time: 9:51
 */

namespace App\Services\LifeCycle;


use App\Admin\Services\CarService;
use App\Models\CarPackage;
use App\Models\CarPackageCategory;
use App\Models\CarPackageType;

class PackageService
{
    public static function all($condition = [])
    {
        $type = $condition['package_type'] ?? PACKAGE_TYPE_SALE;

        $package = CarPackage::where([
            'category_id' => $condition['category_id'],
            'status' => DEFAULT_STATUS_SUCCESS,
            'type' => $type
        ])->get();

        if ($package) {
            return $package;
        }

        return false;
    }

    public static function one($id)
    {
        $package = CarPackage::find($id);

        $package->pic = path2url($package->pic);
        $package->brand_title = $package->brand->title;
        $package->brand_pic = path2url($package->brand->pic);
        $package->category_title = $package->type == PACKAGE_TYPE_SALE ? $package->category->title : '售后'.$package->category->title;
        $package->cars = CarService::carJsonSwitch($package->category->car_ids);

        return $package;
    }

    public static function agreement($package_id)
    {
        return CarPackage::find($package_id, ['agreement']);
    }

    public static function giveAttr($package)
    {
        $give = [];
        $attr = [
            [
                'id' => 0,
                'type_title' => '套餐类别',
                'children' => [
                    [
                        'content' => '增值服务（购买任意套餐赠送）',
                        'checked' => true,
                        'price' => ''
                    ]
                ]
            ]
        ];

        foreach ($package as $key => $value) {
            $attrs = json_decode($value['attrs'], true);

            if ($attrs) {
                foreach ($attrs as $k => $val) {
                    if ($val['type_id'] == 4) {
                        $give['type_title'] = '赠送服务';
                        $give['children'][] = $val;
                    }
                }

                if (isset($give['children'])) {
                    break;
                }
            }
        }

        if (empty($give)) {
            return '';
        } else {
            $give_attr = array_merge($attr, [$give]);

            return $give_attr;
        }
    }

    public static function attrs($attrs, $package_title)
    {
        $attrs = json_decode($attrs, true);

        $attr = [
            [
                'id' => 0,
                'type_title' => '套餐类别',
                'children' => [
                    [
                        'content' => $package_title,
                        'checked' => true,
                        'price' => ''
                    ]
                ]
            ]
        ];

        if ($attrs) {
            $type = CarPackageType::where('status', DEFAULT_STATUS_SUCCESS)->get()->toArray();

            foreach ($attrs as $key => $value) {
                foreach ($type as $k => &$val) {
                    $val['type_title'] = $val['title'];
                    if ($value['type_id'] == $val['id']) {
                        $val['children'][] = $value;
                    }
                }
            }

            foreach ($type as $key => $value) {
                if ($value['id'] == 4 && isset($value['children'])) {
                    unset($type[$key]);
                }

                if (!isset($value['children'])) {
                    unset($type[$key]);
                }
            }

            $attr = array_merge($attr, $type);
        }

        return $attr;
    }
}